package com.orangelife.mobile.goodsOrder.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.util.MoneyUtil2;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.ImageDownloader.ImageSize;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.constants.Constant;

import java.util.List;
import java.util.Map;

public class GoodsOrderListChildrenAdapter<T> extends FatherAdapter<T> {
    private Context context;
    private List<Map<String, Object>> list;

    private ImageDownloader image = ImageDownloader.getImageDownloader();
    private String mViewType;

    public GoodsOrderListChildrenAdapter(Context context, List<Map<String, Object>> list) {
        this.context = context;
        this.list = list;
    }

    public void setViewType(String viewType) {
        this.mViewType = viewType;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_mygoods_order_children, null);
            viewHolder.ivMyGoodsPic = (ImageView) convertView
                    .findViewById(R.id.ivMyGoodsPic);
            viewHolder.tvGoodsTitle = (TextView) convertView
                    .findViewById(R.id.tvGoodsTitle);
            viewHolder.tvGoodsDesc = (TextView) convertView
                    .findViewById(R.id.tvGoodsDesc);
            viewHolder.tvGoodsNum = (TextView) convertView
                    .findViewById(R.id.tvGoodsNum2);
            viewHolder.tvPrice = (TextView) convertView
                    .findViewById(R.id.tvPrice);
            viewHolder.tvFormerPrice = (TextView) convertView
                    .findViewById(R.id.tvFormerPrice);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        Map<String, Object> map = list.get(position);
        image.download(String.valueOf(map.get("cover")),
                viewHolder.ivMyGoodsPic,
                ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
        viewHolder.tvGoodsTitle.setText(String.valueOf(map.get("title")
        ));
        viewHolder.tvGoodsDesc.setText(String.valueOf(map.get("pdtDesc")
        ));
        viewHolder.tvGoodsNum.setText(String.valueOf(map.get("goodsCount")
        ));
        String price = String.valueOf(map.get("price"));
        String mkPrice = String.valueOf(map.get("mkPrice"));
        if ("2".equals(mViewType)) {//积分 2
            price = MoneyUtil2.trimZero(price);
            viewHolder.tvPrice.setText(price + "ml橙汁");
        } else {//金额 1
            viewHolder.tvPrice.setText(Constant.RMB_FLAG + price);
        }

        if (!StringUtil.isContainBlank(mkPrice) && "1".equals(mViewType)) {
            viewHolder.tvFormerPrice.setVisibility(View.VISIBLE);
            viewHolder.tvFormerPrice.setText(Constant.RMB_FLAG
                    + mkPrice);
            viewHolder.tvFormerPrice.getPaint().setFlags(
                    Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            viewHolder.tvFormerPrice.setVisibility(View.GONE);
        }

        return convertView;

    }

    class ViewHolder {
        private ImageView ivMyGoodsPic;
        private TextView tvGoodsTitle;
        private TextView tvGoodsDesc;
        private TextView tvGoodsNum;
        private TextView tvFormerPrice;
        private TextView tvPrice;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        this.list = (List<Map<String, Object>>) list;
        return GoodsOrderListChildrenAdapter.this;
    }

}
